package xc.com.cms.controller;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import xc.com.cms.service.CmsPageService;
import xc.com.web.BaseController;

import javax.servlet.ServletOutputStream;
import java.io.IOException;

@Controller
@CrossOrigin
public class CmsPagePreviewController extends BaseController {
    @Autowired
    private CmsPageService cmsPageService;
    /**
     * id根据获取页面
     * @param pageId
     */
    @GetMapping("/cms/preview/{pageId}")
    public void  preview(@PathVariable("pageId")String pageId){

        String html = cmsPageService.getPageHtml(pageId);
        if(StringUtils.isNotBlank(html)){
            try {
                ServletOutputStream outputStream = response.getOutputStream();
                outputStream.write(html.getBytes("UTF-8"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @GetMapping("/course/preview/{courseId}")
    public void course(@PathVariable("courseId") String courseId){
        String html = cmsPageService.getCourseHtml(courseId);
        if(StringUtils.isNotBlank(html)){
            try {
                response.setContentType("text/html;charset=utf-8");
                IOUtils.write(html,response.getOutputStream(),"utf-8");
                response.getOutputStream().flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
